package com.jichangxiu.flow.server.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.jichangxiu.flow.server.core.entity.Bo.EdgeBo;
import com.jichangxiu.flow.server.core.entity.Bo.NodeBo;
import com.jichangxiu.flow.server.core.entity.dto.Edge;
import com.jichangxiu.flow.server.core.entity.dto.Node;
import com.jichangxiu.flow.server.core.enums.PropertyEnums;

import java.util.List;
import java.util.Optional;

/**
 * LogicFlowUtils:
 *
 * @author Jcx
 * @create 2025-01-01 00:00:00
 */
public class BaseUtils {

    /**
     * 获取节点的链路tag
     *
     * @param nodeBo 节点
     * @return 链路tag
     */
    public static String getTagByNodeBo(NodeBo nodeBo) {
        List<EdgeBo> targetEdgeBoList = nodeBo.getTargetEdgeBoList();
        if (ObjectUtil.isNotEmpty(targetEdgeBoList) || targetEdgeBoList.size() == 1) {
            return getPropertyValue(targetEdgeBoList.get(0), PropertyEnums.tag.name());
        }
        return null;
    }

    /**
     * 获取布尔节点的 true 节点
     *
     * @param childNodeBoList 子节点列表
     * @return true 节点
     */
    public static NodeBo getBooleanTrueNodeBo(List<NodeBo> childNodeBoList) {
        for (NodeBo nodeBo : childNodeBoList) {
            List<EdgeBo> targetEdgeBoList = nodeBo.getTargetEdgeBoList();
            if (ObjectUtil.isNotEmpty(targetEdgeBoList)) {
                for (EdgeBo edgeBo : targetEdgeBoList) {
                    String propertyValue = getPropertyValue(edgeBo, PropertyEnums.value.name());
                    if (propertyValue.equalsIgnoreCase("true")) return nodeBo;
                }
            }
        }
        return null;
    }

    /**
     * 获取布尔节点的 false 节点
     *
     * @param childNodeBoList 子节点列表
     * @return false 节点
     */
    public static NodeBo getBooleanFalseNodeBo(List<NodeBo> childNodeBoList) {
        for (NodeBo nodeBo : childNodeBoList) {
            List<EdgeBo> targetEdgeBoList = nodeBo.getTargetEdgeBoList();
            if (ObjectUtil.isNotEmpty(targetEdgeBoList)) {
                for (EdgeBo edgeBo : targetEdgeBoList) {
                    String propertyValue = getPropertyValue(edgeBo, PropertyEnums.value.name());
                    if (propertyValue.equalsIgnoreCase("false")) return nodeBo;
                }
            }
        }
        return null;
    }

    public static String getPropertyValue(Node node, String propertyName) {
        return Optional.ofNullable(node)
                .map(Node::getProperties)
                .map(e -> e.get(propertyName))
                .map(Object::toString)
                .orElse("");
    }

    public static String getPropertyValue(NodeBo nodeBo, String propertyName) {
        return Optional.ofNullable(nodeBo)
                .map(NodeBo::getProperties)
                .map(e -> e.get(propertyName))
                .map(Object::toString)
                .orElse("");
    }

    public static String getPropertyValue(Edge edge, String propertyName) {
        return Optional.ofNullable(edge)
                .map(Edge::getProperties)
                .map(e -> e.get(propertyName))
                .map(Object::toString)
                .orElse("");
    }

    public static String getPropertyValue(EdgeBo edgeBo, String propertyName) {
        return Optional.ofNullable(edgeBo)
                .map(EdgeBo::getProperties)
                .map(e -> e.get(propertyName))
                .map(Object::toString)
                .orElse("");
    }

    /**
     * 替换key
     *
     * @param key 关键字
     * @return 替换后的关键字
     */
    public static String getReplaceKey(String key) {
        return "<RP>" + key + "</RP>";
    }

    /**
     * 替换key
     *
     * @param key 关键字
     * @return 替换后的关键字
     */
    public static String getCycleReplaceKey(String key) {
        return "<RP_CYCLE>" + key + "</RP_CYCLE>";
    }

    /**
     * 替换key
     *
     * @param key 关键字
     * @return 替换后的关键字
     */
    public static String getCommonReplaceKey(String key) {
        return "<RP_COMMON>" + key + "</RP_COMMON>";
    }

}
